package com.medium.house.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.util.*;

public class StaticConfig {
    private static final Logger logger = LoggerFactory.getLogger(StaticConfig.class);
    // 加载本机IP
    private static final List<String> IPV4_LIST = loadIPs();
    // 加载静态配置变量
    private static final Map<String, Object> configs = loadConfig();

    private StaticConfig() {
    }

    private static Map<String, Object> loadConfig() {
        Map<String, Object> configs = new HashMap<String, Object>();
        try {
            InputStream is = StaticConfig.class.getResourceAsStream("/application.xml");
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = db.parse(new InputSource(is));

            // 初始化静态配置变量
            NodeList nodeList = doc.getElementsByTagName("constant");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String value = element.getAttribute("value");
                    String type = element.getAttribute("type");
                    if("int".equals(type)){
                        configs.put(element.getAttribute("name"), Integer.parseInt(value));
                        break;
                    }else if ("array".equals(type)){
                        configs.put(element.getAttribute("name"), value.split("\\s*,\\s*"));
                        break;
                    }else if ("string".equals(type)){

                    }else {
                        configs.put(element.getAttribute("name"), value);
                    }
                    /*switch (type) {
                        case "int":
                            configs.put(element.getAttribute("name"), Integer.parseInt(value));
                            break;
                        case "array":
                            configs.put(element.getAttribute("name"), value.split("\\s*,\\s*"));
                            break;
                        case "string":
                        default:
                            configs.put(element.getAttribute("name"), value);
                    }*/
                    if ("int".equals(type)){
                        configs.put(element.getAttribute("name"), Integer.parseInt(value));
                        break;
                    }else if ("array".equals(type)){
                        configs.put(element.getAttribute("name"), value.split("\\s*,\\s*"));
                        break;
                    }else if ("string".equals(type)){

                    }else {
                        configs.put(element.getAttribute("name"), value);
                    }
                }
            }
        } catch (Exception e) {
            logger.info("Error occurs in parsing application.xml");
            logger.debug(e.getMessage(), e);
        }
        return configs;
    }

    private static List<String> loadIPs() {
        List<String> ips = NetUtil.getLocalIPs();
        logger.info("Local IPv4 addresses: " + StringUtil.implode(ips, ", "));
        return Collections.unmodifiableList(ips);
    }
    public static String[] getExcelField(){
        String[] excelField = {"realName","idCardNumber","cellphone","bindShopName","bindShopProvince","bindShopCity","bindShopDistrict","spouseName","spouseIdCardNumber","spouseCellphone","gender","hukouProvince","hukouCity","hukouDistrict","hukouTown","hukouVillage","resCountry",
                "resProvince","resCity","resDistrict","resTown","resVillage","address","marriage","children","carPlates","housing","homesteadArea","cultivateLandArea","poultryFarmScale","yearlyWageIncomes","yearlyCultivateIncomes",
                "college","organization","updatedAt","bankCredit","bankCreditNote","houseArea",
                "experienceGold","maxCreditLine","creditAmount","createdAt","appName","regChannel","birthdayTang","nativePlaceTang",
                "birthPlaceTang","addressTang","companyTang","nationTang","formerNameTang","maritalStatusTang","educationTang"};
        return excelField;
    }

    public static String[] getExcelHead(){
        String[] excelHead = {"姓名","身份证号码","手机号码","绑定商家名称","绑定商家所在省","绑定商家所在市","绑定商家所在县","配偶姓名","配偶身份证号码","配偶手机号码","性别","户口所在省","户口所在市","户口所在区县",
                "户口所在乡镇","户口所在村","常住:国家/地区",
                "常住:省","常住:市","常住:区县","常住:乡镇","常住:村","常住:详细地址","婚姻状况","儿女数量","车辆情况","商品房套数","宅基地面积:亩","承包土地类型及面积","养殖动物类型及数量",
                "打工年收入","种植与养殖年收入",
                "教育情况","工作情况","最近提交时间","银行征信材料有无","银行征信材料备注","商品房面积:米","体验金(可用/总计)","最大授信额度","信用额度(可用/总计)","注册时间","关注公众号","注册渠道",
                "出生日期(数据堂)", "籍贯(数据堂)", "出生地(数据堂)","住址(数据堂)","工作单位(数据堂)","民族(数据堂)","曾用名(数据堂)","婚姻状况(数据堂)","文化程度(数据堂)"};
        return excelHead;
    }

    //需要选择导出的列名
    public static Map<String,String> getSelectMap(){
        Map<String,String> items = new LinkedHashMap<String,String>();
        items.put("realName","姓名");
        items.put("idCardNumber","身份证号码");
        items.put("cellphone","手机号码");
        items.put("bindShopName","绑定商家名称");
        items.put("bindShopProvince","绑定商家所在省");
        items.put("bindShopCity","绑定商家所在市");
        items.put("bindShopDistrict","绑定商家所在县");
        items.put("spouseName","配偶姓名");
        items.put("spouseIdCardNumber","配偶身份证号码");
        items.put("spouseCellphone","配偶手机号码");
//        items.put("gender","性别");
        items.put("hukouProvince","户口所在省");
        items.put("hukouCity","户口所在市");
        items.put("hukouDistrict","户口所在区县");
        items.put("hukouTown","户口所在乡镇");
        items.put("hukouVillage","户口所在村");
        items.put("resCountry","常住:国家/地区");
        items.put("resProvince","常住:省");
        items.put("resCity","常住:市");
        items.put("resDistrict","常住:区县");
        items.put("resTown","常住:乡镇");
        items.put("resVillage","常住:村");
        items.put("address","常住:详细地址");
        items.put("marriage","婚姻状况");
        items.put("children","儿女数量");
        items.put("carPlates","车辆情况");
        items.put("housing","商品房套数");
        items.put("homesteadArea","宅基地面积:亩");
        items.put("cultivateLandArea","承包土地类型及面积");
        items.put("poultryFarmScale","养殖动物类型及数量");
        items.put("yearlyWageIncomes","打工年收入");
        items.put("yearlyCultivateIncomes","种植与养殖年收入");
        items.put("college","教育情况");
        items.put("organization","工作情况");
        items.put("updatedAt","最近提交资料时间");
        items.put("bankCredit","银行征信材料有无");
        items.put("bankCreditNote","银行征信材料备注");
        items.put("houseArea","商品房面积:米");
        items.put("experienceGold","体验金(可用/总计)");
        items.put("maxCreditLine","最大授信额度");
        items.put("creditAmount","信用额度(可用/总计)");
        items.put("createdAt","注册时间");
        items.put("birthdayTang","出生日期(数据堂)");
        items.put("nativePlaceTang","籍贯(数据堂)");
        items.put("birthPlaceTang","出生地(数据堂)");
        items.put("addressTang","住址(数据堂)");
        items.put("companyTang","工作单位(数据堂)");
        items.put("nationTang","民族(数据堂)");
        items.put("formerNameTang","曾用名(数据堂)");
        items.put("maritalStatusTang","婚姻状况(数据堂)");
        items.put("educationTang","文化程度(数据堂)");
        items.put("appName","关注公众号");
        items.put("regChannel","注册渠道");
        return items;
    }

    public static Map<String, Object> getConfigs() {
        return configs;
    }

    public static String getConfig(String key) {
        return (String) configs.get(key);
    }

    public static int getIntConfig(String key) {
        return (Integer) configs.get(key);
    }

    public static String[] getArrayConfig(String key) {
        return (String[]) configs.get(key);
    }

    public static boolean inDevelopment() {
        return getIntConfig("in_development") == 1 || getIntConfig("in_development") == 2;
    }
}
